import {Component, EventEmitter,Input, OnInit, Output} from "@angular/core";
import {NzNotificationService, NzModalService} from "ng-zorro-antd";
import {HttpServcie} from "../../../service/http.service";
import {SpotCheckPlan} from "../../../pages/maintenancePlan/bean/spotCheckPlan";
import {SpotCheck} from "../../../pages/maintenancePlan/spot-check/spot-check";
import * as moment from "moment";


@Component({
  selector: "spot-check-add",
  templateUrl: "spot-check-add.html",
  styleUrls: ["spot-check-add.scss"]
})
export class SpotCheckAdd implements OnInit {

  @Input()
  inValue: any = {};

  // 定义父子传值事件
  @Output("outPut")
  public outPut = new EventEmitter();
  // 定义对象
  public spotCheckPlan:SpotCheckPlan = new SpotCheckPlan();
  // 定义开始时间
  public startTime:any=null;
  // 定义结束时间
  public endTime:any=null;
  // 定义当前登录人的信息
  public user:any;
  // 定义实施人和责任人的信息
  public objects:any=[];
  // 定义实施单位
  public companyEn:any;
  // 定义实施对象选中数据
  public objectList:any=[];

  constructor(
    private potCheck:SpotCheck,
    private _notification: NzNotificationService,
    private httpServcie: HttpServcie
  ) {
  }

  ngOnInit() {
    this.user = JSON.parse(window.localStorage.getItem("user"));
    this.companyEn = this.user.partyName;
    console.log(this.user);
    this.queryMachine();
  }

  // 添加方法
  submitForm($event) {

    if(this.spotCheckPlan.planName ==undefined ){
      this._notification.create("warning", "警告提示", "计划名称不能为空！");
      return false;
    }

    if(this.startTime ==null ){
      this._notification.create("warning", "警告提示", "开始时间不能为空！");
      return false;
    }else{
      this.spotCheckPlan.planStartTime = this.startTime ? moment(this.startTime).format("YYYY-MM-DD") : "";
    }
    if(this.endTime ==null ){
      this._notification.create("warning", "警告提示", "结束时间不能为空！");
      return false;
    }else{
      this.spotCheckPlan.planEndTime = this.endTime ? moment(this.endTime).format("YYYY-MM-DD") : "";
    }
    if(this.endTime <= this.startTime ){
      this._notification.create("warning", "警告提示", "结束时间不能小于或等于开始时间！");
      return false;
    }

    // 创建人
    this.spotCheckPlan.createBy = JSON.parse(window.localStorage.getItem("user")).userName;
    // 制定单位
    this.spotCheckPlan.company = this.companyEn;
    //单位名称Id
    if(this.user.partyId ==null || this.user.partyId == undefined){
      this._notification.create("warning", "警告提示", "您已登录超时，请刷新重试！");
      return false;
    }
    this.spotCheckPlan.partyId =  this.user.partyId;


    this.httpServcie.insertSpotPlan(this.spotCheckPlan).subscribe(data => {
        if (data.status == 200) {
          this._notification.create("success", "成功", "新增成功！");
          this.outPut.emit($event);
        } else {
          this._notification.create("error", "错误提示", data.message);
        }
      },
      error => {
        this._notification.create("error", "错误提示", "删除失败！");
      }
    )
  }

  // 根据当前得维保单位查询维保人员
  queryMachine() {
    this.httpServcie.queryUserByPartyId(this.user.partyId).subscribe(data => {
      if (data.status == 200) {
        if (data.data && data.data.length > 0) {
          console.log(data.data);
          this.objects = data.data;
        }
      }
    });
  }


}
